//
//  DLChosePswTypeView.m
//  DontRem
//
//  Created by apple on 2021/1/20.
//

#import "DLChosePswTypeView.h"

#define roundRadius 5
#define angleHeight 6
#define angleWidth 10

@interface DLChosePswTypeView()
@property(nonatomic,assign)CGPoint poi;
@property(nonatomic,assign)CGFloat v_width ,v_height;
@property(nonatomic,retain)UILabel * pswCountLab;
@end

@implementation DLChosePswTypeView

- (instancetype)initWithFrame:(CGRect)frame point:(CGPoint)poi
{
    self = [super initWithFrame:frame];
    if (self) {
        self.poi = poi;
        self.v_width = SCREEN_WIDTH * 0.6;
        self.v_height = 120;
        [self addTarget:self action:@selector(dismissSelf)];
        
        [self drawShape];
        [self createUI];
    }
    return self;
}
-(void)dismissSelf{
    if (self.dismissBlock) {
        self.dismissBlock();
    }
}
-(void)createUI{
    UILabel * titleLab = [UILabel labelWithTitle:@"设置密码位数" color:TextColor fontSize:15];
    [self addSubview:titleLab];
    [titleLab sizeToFit];
    titleLab.frame = Frame(15 + roundRadius, self.poi.y + kTopHeight - angleHeight - 120 + roundRadius, titleLab.mm_w, titleLab.mm_h);
    
    UISlider * slider = [[UISlider alloc]initWithFrame:Frame(titleLab.mm_minX, titleLab.mm_maxY + 5, self.v_width - roundRadius* 2 - 40-5, 35)];
    slider.minimumValue = 8;
    slider.maximumValue = 16;
    [slider addTarget:self action:@selector(sliderValueDidChanged:) forControlEvents:UIControlEventValueChanged];
    [self addSubview:slider];
    slider.value = [DLUserDefaultModel userDefaultsModel].pswCount;
    
    int count = [DLUserDefaultModel userDefaultsModel].pswCount > 0 ? [DLUserDefaultModel userDefaultsModel].pswCount : 8;
    NSString * countstr = [NSString stringWithFormat:@"%d",count];
    self.pswCountLab = [UILabel labelWithTitle:countstr color:TextColor fontSize:15];
    [self addSubview:self.pswCountLab];
    self.pswCountLab.mm_width(40).mm_height(35).mm_left(slider.mm_maxX + 5).mm_top(slider.mm_minY);
    
    NSArray * arr = @[@"数字",@"字母",@"符号"];
    CGFloat wid = floor((self.v_width - roundRadius * 2)/3.0);
    DLUserDefaultModel * def = [DLUserDefaultModel userDefaultsModel];
    NSArray * boolArr = @[@(def.choseNum),@(def.choseAlpha),@(def.choseSymbol)];
    for (int i =0; i<arr.count ; i++) {
        QMUIButton * btn = [QMUIButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:arr[i] forState:UIControlStateNormal];
        [btn setTitleColor:TextColor forState:UIControlStateNormal];
        btn.titleLabel.font = UIFontMake(15);
        [btn setImagePosition:QMUIButtonImagePositionLeft];
        btn.spacingBetweenImageAndTitle = 5;
        [btn setImage:Image(@"方框_nor_20") forState:UIControlStateNormal];
        [btn setImage:Image(@"方框_sel_20") forState:UIControlStateSelected];
        btn.frame = Frame(15+roundRadius + wid*i, slider.mm_maxY + 10, wid, 35);
        BOOL bl = [boolArr[i] boolValue];
        btn.selected = bl;
        btn.tag = 10 + i;
        [btn addTarget:self action:@selector(choseTypeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
    }
    
}
-(void)choseTypeBtnClick:(QMUIButton*)sender{
    sender.selected = !sender.selected;
    if (sender.tag == 10) {
        [DLUserDefaultModel userDefaultsModel].choseNum = sender.isSelected;
    }else if(sender.tag ==11){
        [DLUserDefaultModel userDefaultsModel].choseAlpha = sender.isSelected;
    }else{
        [DLUserDefaultModel userDefaultsModel].choseSymbol = sender.isSelected;
    }
}
-(void)sliderValueDidChanged:(UISlider*)slider{
    int val = round(slider.value);
    self.pswCountLab.text = [NSString stringWithFormat:@"%d",val];
    slider.value = val;
    [DLUserDefaultModel userDefaultsModel].pswCount = val;
}
-(void)drawShape{
    CGFloat startY = self.poi.y+kTopHeight;
    UIBezierPath * path = [UIBezierPath new];
    [path moveToPoint:CGPointMake(self.poi.x, startY)];
    [path addLineToPoint:CGPointMake(self.poi.x - angleWidth/2.f, startY - angleHeight)];
    [path addLineToPoint:CGPointMake(15+roundRadius, startY - angleHeight)];
    [path addArcWithCenter:CGPointMake(15 + roundRadius, startY - angleHeight-roundRadius) radius:roundRadius startAngle:M_PI_2 endAngle:M_PI clockwise:YES];
    [path addLineToPoint:CGPointMake(15,  startY - angleHeight - self.v_height+roundRadius)];
    [path addArcWithCenter:CGPointMake(15 + roundRadius, startY - angleHeight - self.v_height+roundRadius) radius:roundRadius startAngle:M_PI endAngle:3*M_PI_2 clockwise:YES];
    [path addLineToPoint:CGPointMake(15 + self.v_width - roundRadius, startY - angleHeight - self.v_height)];
    [path addArcWithCenter:CGPointMake(15 + self.v_width - roundRadius, startY - angleHeight - self.v_height+roundRadius) radius:roundRadius startAngle:3*M_PI_2 endAngle:2*M_PI clockwise:YES];
    [path addLineToPoint:CGPointMake(15 + self.v_width, startY-angleHeight -roundRadius)];
    [path addArcWithCenter:CGPointMake(15 + self.v_width - roundRadius, startY-angleHeight -roundRadius) radius:roundRadius startAngle:0 endAngle:M_PI_2 clockwise:YES];
    [path addLineToPoint:CGPointMake(self.poi.x + angleWidth/2.0, startY - angleHeight)];
    [path addLineToPoint:CGPointMake(self.poi.x, startY)];
        
    CAShapeLayer * layer = [CAShapeLayer new];
    layer.path=path.CGPath;
    layer.fillColor = UIColorWhite.CGColor;
    layer.frame = self.bounds;
    layer.shadowColor = [UIColor lightGrayColor].CGColor;
    layer.shadowOffset = CGSizeMake(0, 0);
    layer.shadowRadius = 3;
    layer.shadowOpacity = 0.7;
    layer.shadowPath = path.CGPath;
    [self.layer addSublayer:layer];
    
}
@end
